package com.example.demo.algorithm.linear;

import java.util.Iterator;

/**
 * @Author: xiatian
 * @CreateTime: 2021/9/15 10:13 上午
 * @Description:
 */
public class LinkList<T> implements Iterable<T> {
    //记录链表的长度
    private int N;
    //记录头结点
    private Node head;


    public LinkList() {
        head = new Node(null, null);
        N = 0;
    }

    /**
     * 清空链表
     */
    public void clear() {
        head.next = null;
        N = 0;
    }

    /**
     * 判断链表是否为空
     *
     * @return
     */
    public boolean isEmpty() {
        return N == 0;
    }

    /**
     * 返回链表的长度
     *
     * @return
     */
    public int length() {
        return N;
    }

    /**
     * 获取i处的元素T
     *
     * @param
     * @return
     */
    public T get(int i) {
        Node t = head.next;
        for (int index = 0; index < i; index++) {
            t = head.next;
        }
        return t.item;
    }

    /**
     * 在链表后面插入元素
     *
     * @param t
     */
    public void insert(T t) {
        Node n = head;
        while (n.next != null) {
            n = n.next;
        }
        Node newNode = new Node(t, null);
        n.next = newNode;
        //链表长度加1
        N++;
    }

    /**
     * 在指定位置插入元素
     *
     * @param i
     * @param t
     */
    public void insert(int i, T t) {

        //寻找位置i之前的结点
        Node pre = head;
        for (int index = 0; index <= i - 1; index++) {
            pre = pre.next;
        }
        Node curr = pre.next;
        Node newNode = new Node(t, curr);
        pre.next = newNode;
        N++;
    }

    /**
     * 删除并返回线性表中第i个数据元素
     *
     * @param i
     * @return
     */
    public T remove(int i) {
        Node pre = head;
        for (int index = 0; index <= i - 1; index++) {
            pre = pre.next;
        }
        //当前i位置的结点
        Node curr = pre.next;
        //前一个结点指向下一个结点，删除当前结点
        pre.next = curr.next;
        //长度-1
        N--;
        return curr.item;
    }

    /**
     * 返回线性表中首次出现的指定的数据元素的位序号，若不存在，则返回-1
     *
     * @param t
     * @return
     */
    public int indexOf(T t) {
        Node n = head;
        for (int i = 0; n.next != null; i++) {
            n = n.next;
            if (n.item.equals(t)) {
                return i;
            }
        }
        return -1;
    }

    public void reverse() {
        if (0 == N) {
            return;
        }
        reverse(head.next);
    }

    /**
     * 反转链表中的某个结点curr,并把反转后的curr结点返回
     *
     * @param curr
     * @return
     */
    public Node reverse(Node curr) {
        //最后一个节点
        if (curr.next == null) {
            head.next = curr;
            System.out.println("最后一个节点是："+curr.item);
            return curr;
        }
        //不是最后一个节点
        Node pre = reverse(curr.next);
        System.out.println("当前节点是 "+curr.item+"上一个节点是："+pre.item);
        pre.next = curr;
        curr.next = null;
        return curr;
    }

    @Override
    public Iterator<T> iterator() {
        return new LIterator();
    }

    private class LIterator implements Iterator<T> {
        private Node n;

        public LIterator() {
            this.n = head;
        }

        @Override
        public boolean hasNext() {
            return n.next != null;
        }

        @Override
        public T next() {
            n = n.next;
            return n.item;
        }
    }


    private class Node {
        T item;
        Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }

}
